//
// ListResponder
//
// Copyright (C) jextra.net.
//
//  This file is part of the Nubbin Library.
//
//  The Nubbin Library is free software; you can redistribute it and/or
//  modify it under the terms of the GNU Lesser General Public
//  License as published by the Free Software Foundation; either
//  version 2.1 of the License, or (at your option) any later version.
//
//  The Nubbin Library is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
//  Lesser General Public License for more details.
//
//  You should have received a copy of the GNU Lesser General Public
//  License along with the Nubbin Library; if not, write to the Free
//  Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
//  02111-1307 USA.
//

package net.jextra.nubbinstoreweb;

import com.javapepper.core.*;
import java.io.*;
import javax.servlet.http.*;
import net.jextra.nubbin.store.http.*;

public class ListResponder extends BaseResponder
{
    // ============================================================
    // Fields
    // ============================================================

    private StorePathMapper _pathMap;
    private HttpServletResponse _response;

    // ============================================================
    // Methods
    // ============================================================

    // ----------
    // public
    // ----------

    @Override
    public void processRequest( PepSession pepSession, PepMethod method,
        HttpServletRequest request, HttpServletResponse response )
        throws Exception
    {
        _pathMap = new StorePathMapper( request );
        System.out.println( "ListResponder " + _pathMap.getType() + " " +
            _pathMap.getComponentName() );
        _response = response;

        _response.setContentType( "text/xml" );
        switch ( _pathMap.getType() )
        {
            case components:
                listComponents();
                break;

            case versions:
                listVersions();
                break;

            default:
                listEntries();
                break;
        }
    }

    // ----------
    // private
    // ----------

    private void listComponents()
        throws Exception
    {
        NubbinStoreComponentsWriter writer = new NubbinStoreComponentsWriter();
        writer.write( _response.getOutputStream(), getStore().getComponents() );
    }

    private void listVersions()
        throws Exception
    {
        NubbinStoreVersionsWriter writer = new NubbinStoreVersionsWriter();
        writer.write( _response.getOutputStream(), _pathMap.getComponentName(),
            getStore().getLatestVersionNumber( _pathMap.getComponentName() ),
            getStore().getNextVersionNumber( _pathMap.getComponentName() ), getStore().getVersions(
            _pathMap.getComponentName() ) );
    }

    private void listEntries()
        throws IOException, Exception
    {
        NubbinStoreEntriesWriter writer = new NubbinStoreEntriesWriter();
        writer.write( _response.getOutputStream(), _pathMap.getComponentName(),
            _pathMap.getVersionNumber(), _pathMap.getPath(), getStore().getEntries(
            _pathMap.getComponentName(), _pathMap.getVersionNumber(), _pathMap.getPath() ) );
    }
}

